<template>
	<swiper class="home-swiper" @change="change" :current="activeIndex">
		<swiper-item class="swiper-item" v-for="(item, index) in tab" :key="index">
			<list-item :list="listCatchData[index]" @loadMore="loadMore" :load="load[index]"></list-item>
		</swiper-item>
		<!-- <swiper-item  class="swiper-item">
		  <list-item></list-item>
		</swiper-item>
		<swiper-item class="swiper-item">
		  <list-item></list-item>
		</swiper-item> -->
	</swiper>
</template>

<script>
	import listItem from './list-item'
	export default {
		components: {
			listItem
		},
		props: {
			tab: {
				type: Array,
				default: () => {
					return []
				}
			},
			activeIndex: {
				type: Number,
				default: 0
			}
		},
		watch: {
			activeIndex(newVal) {
				// console.log(newVal)
			},
			tab(newVal) {
				if (newVal.length === 0) return;
				this.listCatchData = {}
				this.load = {}
				this.getList(this.activeIndex)
			}
		},
		data() {
			return {
				// list:[],
				listCatchData: {},
				load:{
				},
				pageSize:10
			};
		},
		methods: {
			change(e) {
				const {
					current
				} = e.detail;
				// console.log(this.tab[current])
				// console.log(e)
				this.$emit('change', current)
				//判断数据是否缓存
				if (!this.listCatchData[current] || this.listCatchData[current].length === 0) {
					this.getList(current)
				}
			},
			getList(current) {
				if(!this.load[current]) {
					this.load[current] = {
						page:1,
						loading:'loading'
					}
				}
				this.$api.get_list({
					name: this.tab[current].name,
					page: this.load[current].page,
					pageSize: this.pageSize
				}).then(res => {
					let {data} = res
					// this.list = res.data
					if(data.length === 0){
						let oldLoad = {}
						oldLoad.loading = 'noMore'
						oldLoad.page = this.load[current].page
						this.$set(this.load,current,oldLoad)
						this.$forceUpdate()
						return
					}
					let oldList = this.listCatchData[current] || [];
					oldList.push(...data)
					//懒加载
					this.$set(this.listCatchData, current, oldList)
					// console.log(this.listCatchData)
				})
			},
			loadMore(){
				console.log('loadMore', this.load[this.activeIndex].loading === 'noMore' )
				if(this.load[this.activeIndex].loading === 'noMore') return
				console.log('请求数据')
				this.load[this.activeIndex].page++
				this.getList(this.activeIndex)
				// console.log("上拉")
			}
		},
		created() {
			// this.getList('后端开发')
		},
		mounted() {
			// console.log(this.tabList)
		}
	}
</script>

<style lang="scss">
	.home-swiper {
		height: 100%;

		.swiper-item {
			height: 100%;
			overflow: hidden;

			.list-scroll {
				height: 100%;
			}
		}
	}
</style>
